package com.acc.store.service.impl;

import com.acc.store.entity.Category;
import com.acc.store.entity.Inventory;
import com.acc.store.entity.Product;
import com.acc.store.mapper.ProductMapper;
import com.acc.store.service.ProductService;
import com.acc.store.vo.ProductVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 10067
 * @description 针对表【product(商品表)】的数据库操作Service实现
 * @createDate 2023-10-13 18:05:14
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    @Override
    public IPage getPage(Map<String, Object> params) {
        //提取参数
        Long page = params.get("page") != null && !"null".equals(params.get("page")) ? Long.valueOf((String) params.get("page")) : null;
        Long limit = params.get("limit") != null && !"null".equals(params.get("limit")) ? Long.valueOf((String) params.get("limit")) : null;
        Object key = params.get("key");
        Long categoryId = params.get("categoryId") != null && !"null".equals(params.get("categoryId")) ? Long.valueOf((String) params.get("categoryId")) : null;   //封装查询语句
        Page<Product> page1 = null;
        LambdaQueryChainWrapper<Product> wrapper = lambdaQuery();
        if (key!=null&&!String.valueOf(key).isEmpty() ){
            wrapper = wrapper.like(Product::getName, key);
        }
        if (categoryId != null) {
            wrapper.eq(Product::getCategoryId, categoryId);
        }
        page1 = wrapper.page(new Page<>(page, limit));
        List<ProductVo> productVos = page1.getRecords().stream().map(item -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(item, productVo);
            productVo.setCategoryName(Db.getById(item.getCategoryId(), Category.class).getCategoryName());
            setStatus(productVo);
            return productVo;
        }).toList();
        Page<ProductVo> voPage = new Page<>();
        voPage.setPages(page1.getPages());
        voPage.setRecords(productVos);
        voPage.setTotal(page1.getTotal());
        voPage.setCurrent(page1.getCurrent());
        return voPage;
    }

    private void setStatus(ProductVo productVo) {
        List<Inventory> list = Db.lambdaQuery(Inventory.class).eq(Inventory::getProductId, productVo.getProductId()).list();
        Integer num = 0;
        for (Inventory inventory : list) {
            num = inventory.getQuantityInStock() + num;
        }
        if (num > 0) {
            productVo.setStatus(0);
        } else {
            productVo.setStatus(1);
        }
    }

    @Override
    public ProductVo getVoById(Long id) {
        Product product = getById(id);
        if (product == null) {
            return null;
        }
        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product, productVo);
        Category category = Db.getById(product.getCategoryId(), Category.class);
        productVo.setCategoryName(category.getCategoryName());

        return productVo;
    }

    @Override
    public Product getProduct(Long barcode) {
        Product product = lambdaQuery().eq(Product::getBarcode, barcode).one();
        return product;
    }
}




